package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.AddressBook;

import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
@Transactional
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>implements AddressBookService {


    @Autowired
    private AddressBookMapper mapper;

   /**
     * 修改地址
     *
     * @param addressBook
     */
    @Override
    public void updateAddress(AddressBook addressBook) {
      //  mapper.updateById(addressBook);
    }

    /**
     * 删除地址
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            for (Long id : ids) {
                LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(AddressBook::getId, id);
                wrapper.eq(AddressBook::getIsDefault, 1);//地址是否是为默认状态 如果是不能删除
                AddressBook addressBook = mapper.selectOne(wrapper);
                if (addressBook != null) {
                    throw new CustomException("地址[" + addressBook.getDetail() + "]为默认地址，不能删除");
                }
                mapper.deleteBatchIds(ids);
            }
        }
    }
}
